/**
 * 验证昵称是否注册
 * @type {Vue}
 */
let check_nickname = new Vue({
    el: "#nickname",
    data: {},
    mounted:function(){
        var token=handleLocalStorage("get", "token");
      if (token){
          alert("当前已登录，请勿重复登录！");
          window.location.href = "/";
      }
    },
    methods: {
        check_nickname() {
            let nickname = $("#nickname").val();
            if (nickname == null || nickname == "") {
                return;
            }
            axios({
                url: "/user/check",
                method: "post",
                data: {
                    "nickname": nickname,
                },
                headers: {
                    "Content-Type": "application/json"
                }
            }).then(function (response) {
                if (response.data.code !== 10) {
                    alert(response.data.message)
                }
            }).catch(function (error) {
                    alert("服务器异常！");
                });
        }
    }
});

/**
 *  验证邮箱是否注册
 * @type {Vue}
 */
let check_email = new Vue({
    el: "#email",
    data: {},
    methods: {
        check_email() {
            let email = $("#email").val();
            if (email == null || email == "") {
                return;
            }
            axios({
                url: "/user/check",
                method: "post",
                data: {
                    "email": email,
                },
                headers: {
                    "Content-Type": "application/json"
                }
            }).then(function (response) {
                if (response.data.code !== 8) {
                    alert(response.data.message)
                }
            })
                .catch(function (error) {
                    alert("服务器异常！");
                });
        }
    }
});

/**
 * 用户注册
 * @type {Vue}
 */
let user_register = new Vue({
    el: "#register",
    data: {},
    methods: {
        register() {
            let nickname = $("#nickname").val();
            let email = $("#email").val();
            let password = $("#password").val();
            if (nickname == null || nickname == "") {
                alert("昵称不能为空！");
                return;
            }
            if (email == null || email == "") {
                alert("邮箱不能为空！");
                return;
            }
            axios({
                url: "/user/register",
                method: "post",
                data: {
                    "nickname": nickname,
                    "email": email,
                    "password": password,
                },
                headers: {
                    "Content-Type": "application/json"
                }
            }).then(function (response) {
                if (response.data.code === 6) {
                    $("#nickname").val("");
                    $("#email").val("");
                    $("#password").val("");
                    alert(response.data.message);
                } else {
                    alert(response.data.message)
                }
            }).catch(function (error) {
                alert("服务器异常！");
            });
        }
    }
});


/**
 * 用户登录
 * @type {Vue}
 */
let user_login = new Vue({
    el: "#login",
    data: {},
    methods: {
        login() {
            let email = $("#loginEmail").val();
            let password = $("#loginPassword").val();
            if (email == "") {
                alert("请输入邮箱");
                return
            }
            if (password == "") {
                alert("请输入密码");
                return;
            }
            axios({
                url: "/user/login",
                method: "post",
                data: {
                    "email": email,
                    "password": password
                },
                headers: {
                    "Content-Type": "application/json"
                }
            }).then(function (response) {
                if (response.data.code==200){
                    let token = response.data.data.token;
                    let nickname = response.data.data.nickname;
                    handleLocalStorage("set", "token", token);
                    handleLocalStorage("set", "nickname", nickname);
                    window.location.href = "/";
                }else {
                    alert(response.data.message);
                }
            }).catch(function (error) {
                    alert("服务器异常！");
                });
        }
    }
});

